﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace KCommon
{
    public class Mp3Player
    {
        public Mp3Player(string filePlayer)
        {
            FilePath = filePlayer;
        }
        /// <summary>   
        /// 文件地址   
        /// </summary>   
        private string FilePath;

        /// <summary>   
        /// 播放   
        /// </summary>   
        public void Play()
        {
            string path = Directory.GetCurrentDirectory();
            if (File.Exists(FilePath))
            {
                mciSendString("close all", "", 0, 0);
                mciSendString("open " + FilePath + " alias media", "", 0, 0);
                mciSendString("play media", "", 0, 0);
            }
        }

        /// <summary>
        /// 播放声音文件
        /// </summary>
        /// <param name="file"></param>
        public static void Play(string file)
        {
            if (File.Exists(file))
            {
                mciSendString("close all", "", 0, 0);
                mciSendString("open " + file + " alias media", "", 0, 0);
                mciSendString("play media", "", 0, 0);
            }
        }

        /// <summary>   
        /// 暂停   
        /// </summary>   
        public void Pause()
        {
            mciSendString("pause media", "", 0, 0);
        }

        /// <summary>   
        /// 停止   
        /// </summary>   
        public void Stop()
        {
            mciSendString("close media", "", 0, 0);
        }

        /// <summary>   
        /// API函数   
        /// </summary>   
        [DllImport("winmm.dll", EntryPoint = "mciSendString", CharSet = CharSet.Auto)]
        private static extern int mciSendString(
            string lpstrCommand,
            string lpstrReturnString,
            int uReturnLength,
            int hwndCallback
        );   
    }
}
